#include "irm_objectrequest.h"
#include "irm_types.h"

#include <assert.h>


namespace ophies {

	ObjectRequest::ObjectRequest(
		QString controllerName,
		RequestCallback *callback,
		QObject *parent) :
	m_version("v1"),
	m_controllerName(controllerName),
	m_search(NULL),
	m_request(new QNetworkRequest()),
	m_requestCallback(callback),
	//    m_cookieJar(new QNetworkCookieJar()),
	m_manager(new QNetworkAccessManager(this)),
	QObject(parent)
	{
		connect(m_manager, SIGNAL(finished(QNetworkReply*)),
			m_requestCallback, SLOT(onFinished(QNetworkReply*)));
		connect(m_manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
			m_requestCallback, SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

		//    m_manager->setCookieJar(m_cookieJar);
		this->setAuth(USER_NAME, PASSWORD);
	}

	ObjectRequest::ObjectRequest(
        QString controllerName,
        RequestCallback *callback,
		Search* searchOptions,
		QObject *parent) :
    m_version("v1"),
    m_controllerName(controllerName),
    m_search(searchOptions),
    m_request(new QNetworkRequest()),
    m_requestCallback(callback),
//    m_cookieJar(new QNetworkCookieJar()),
    m_manager(new QNetworkAccessManager(this)),
	QObject(parent)
{
    connect(m_manager, SIGNAL(finished(QNetworkReply*)),
            m_requestCallback, SLOT(onFinished(QNetworkReply*)));
    connect(m_manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
            m_requestCallback, SLOT(onAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

//    m_manager->setCookieJar(m_cookieJar);
    this->setAuth(USER_NAME, PASSWORD);
}


ObjectRequest::~ObjectRequest()
{
    std::cout << "ObjectRequest: " << m_controllerName.toStdString() << " destroyed";
    //delete m_params; // param is supposed to be stack variable
    delete m_request;
    delete m_manager;
}

QString ObjectRequest::buildPath(QString id, QString action)
{
    QString path = QString("%1/%2").arg(m_version, m_controllerName);
    if(id.length() != 0)
        path += "/" + id;
    if(action.length() != 0)
        path += "/" + action;


    path += ".json";

    return path;
}

void ObjectRequest::setAuth(QString tokenId, QString secret)
{
    QByteArray auth;
    QString cred = tokenId.append(QString(":"));
    cred.append(secret);

    QByteArray b = cred.toLocal8Bit().toBase64();

    auth.append(QString("Basic "));
    auth.append(QString(b).toLocal8Bit());
    m_request->setRawHeader("Authorization", auth);
}

void ObjectRequest::setParam(QString key, QString value)
{
	assert(FALSE);
    //m_params->insert(key, value); // TODO
	//m_search->insert(key, value);
}

void ObjectRequest::listObject()
{
	QString address = QString(URL) + "/" + buildPath();
    QUrl url(address);

	if(m_search)
	{
		m_search->setQueryTo(url);
	}

    m_request->setUrl(url);

	QString url_str = m_request->url().toString();
    std::cout << m_request->url().toString().toStdString() << std::endl;
	int b = m_manager->networkAccessible();
	std::cout << "network accesible (supposed to be 1): " << b << std::endl;

    m_manager->get(*m_request);	
}


void ObjectRequest::getObject(QString id)
{
    QUrl url = m_request->url();
    url.setUrl(URL);
    url.setPath(buildPath(id));

    m_request->setUrl(url);

    std::cout << m_request->url().toString().toStdString() << std::endl;
    m_manager->get(*m_request);
}

}
